import Vue from 'vue';
import VueRouter from 'vue-router';
import Layout from '@/layouts';
Vue.use(VueRouter);

//基础路由
export const baseRoute = [
    {
        path: '/',
        name:'layout',
        component: Layout,
        redirect: '/home/index',
        hidden: true,
        children: []
    },
    {
        name: 'login',
        path: '/:type/login',
        component: () => import(/* webpackChunkName: 'login' */ '@/views/login/index'),
        hidden: true
    },
    {
        path: '/403',
        component: () => import(/* webpackChunkName: '403' */ '@/views/error/403'),
        hidden: true
    },
    {
        path: '/404',
        component: () => import(/* webpackChunkName: '404' */ '@/views/error/404'),
        hidden: true
    },
    {
        path: '/500',
        component: () => import(/* webpackChunkName: '500' */ '@/views/error/500'),
        hidden: true
    }
];

const createRouter = function() {
    return new VueRouter({
        routes: baseRoute,
        mode: 'history',
        scrollBehavior: () => ({ y: 0 })
    });
};

const router = createRouter();

export function resetRouter() {
    router.matcher = createRouter().matcher;
}

//重定向时报错，用这个不让他报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch(err => err);
};

export default router;
